<template>
  <div id="app">
    <!-- Header -->
    <header class="header can-sticky">
      <el-row type="flex" justify="space-between" align="middle" class="container">
        <el-col :span="4">
          <div class="logo">
            <a href="/home">
              <img src="@/assets/images/logo.png" alt="logo" />
            </a>
          </div>
        </el-col>
        <el-col :span="16">
          <nav class="navigation">
            <ul class="main-menu">
              <li
                v-for="(item, index) in mainMenu"
                :key="index"
                class="menu-item"
                :class="{ 'menu-item-has-children': !!item.children }"
              >
                <a :href="item.href" @click="handleMenuClick(item, $event)">
                  {{ item.title }}
                </a>

                <!-- 二级菜单 -->
                <ul v-if="item.children" class="sub-menu">
                  <li
                    v-for="(child, childIdx) in item.children"
                    :key="childIdx"
                    class="menu-item"
                    :class="{ 'menu-item-has-children': !!child.children }"
                  >
                    <a :href="child.href" @click="handleMenuClick(child, $event)">
                      {{ child.title }}
                    </a>

                    <!-- 三级菜单 -->
                    <ul v-if="child.children" class="sub-menu">
                      <li
                        v-for="(grandChild, gIdx) in child.children"
                        :key="gIdx"
                        class="menu-item"
                      >
                        <a :href="grandChild.href">{{ grandChild.title }}</a>
                      </li>
                    </ul>
                  </li>
                </ul>
              </li>
            </ul>
          </nav>
        </el-col>
<!--        <el-col :span="4" class="text-right">-->
<!--          <a href="/login" class="header_btn thm-btn btn-rounded btn-small">Log In</a>-->
<!--        </el-col>-->
      </el-row>
    </header>

    <!-- Subheader  -->
    <div class="section-bg section-padding subheader">
      <el-container>
        <el-main>
          <div class="container">
            <el-row type="flex" justify="center">
              <el-col :span="24" class="text-center">
                <h1 class="text-white page-title">Blog Details</h1>
              </el-col>
            </el-row>
          </div>
        </el-main>
      </el-container>
    </div>

    <!-- Blog Details Start -->
    <section class="section-padding">
      <div class="container">
        <div class="row">
          <div class="col-12">
            <div class="blog-article">
              <!-- 封面图 -->
              <div class="article-cover" v-if="form.coverImage">
                <img
                  :src="fixImgUrl(form.coverImage)"
                  :style="{ width: '100%', maxHeight: '600px', objectFit: 'cover' }"
                />
              </div>

              <!-- 文章标题区 -->
              <header class="article-header">
                <h1 class="article-title">{{ form.title }}</h1>
                <p class="article-subtitle" v-if="form.subtitle">{{ form.subtitle }}</p>

                <!-- 元信息 -->
                <div class="article-meta">
                  <span>客户：{{ form.clientName || '未知' }}</span>
                  <span>服务类别：{{ form.serviceCategory || '未分类' }}</span>
                  <!--            <span>项目周期：{{ form.projectDuration || '未填写' }}</span>-->
                  <span>发布时间：{{ parseTime(form.createdAt, '{y}-{m}-{d}') }}</span>
                </div>
              </header>

              <!-- 正文内容 -->
              <div class="article-content" v-html="form.content">
                <!-- 富文本内容将在此渲染 -->
              </div>

              <!-- 附加信息（可折叠或小字） -->
              <footer class="article-footer">
                <div class="stats">
                  <small>阅读量：{{ form.viewCount }} | {{ form.isPublished ? '已发布' : '草稿' }} | {{ form.isFeatured ? '首页推荐' : '普通案例' }}</small>
                </div>
                <div class="meta-info" style="font-size: 12px; color: #999; margin-top: 10px;">
                  备注: {{ form.remark || '无' }}
                </div>
              </footer>
            </div>

                <!-- 分享 -->
                <div class="post_share">
                  <h6>Share: </h6>
                  <ul class="social">
                    <li v-for="(social, index) in socialLinks" :key="index">
                      <a :href="social.url">
                        <i :class="social.icon"></i>
                      </a>
                    </li>
                  </ul>
                </div>
              </div>
              <!-- Post Tags & Share End -->
            </div>
          </div>
    </section>
  </div>
</template>

<script>
import '@fortawesome/fontawesome-free/css/all.css'
import { listCases, getCases } from "@/api/achievement/cases"
export default {
  data() {
    return {
      // 社交媒体链接
      socialLinks: [
        { icon: "fab fa-facebook-f", url: "#" },
        { icon: "fab fa-twitter", url: "#" },
        { icon: "fab fa-instagram", url: "#" },
        { icon: "fab fa-behance", url: "#" },
      ],
      // 表单参数
      form: {
        content: ''
      },
      casesList: {},
      loading: false,
      error: null,
      totalCount: 0,
      currentSlide: 0, // 当前激活的轮播索引
      mainMenu: [
        {
          title: '首页',
          href: '/home'
        },
        {
          title: '关于',
          href: '/about'
        },
        {
          title: '服务',
          href: '/services',
        },
        {
          title: '博客',
          href: '#',
          children: [
            {
              title: '博客网络',
              href: '#',
              children: [
                { title: '全宽', href: '/blog-grid' },
                { title: '左侧边栏', href: '/blog-grid-left' },
                { title: '右侧边栏', href: '/blog-grid-right' }
              ]
            },
          ]
        },
        {
          title: '联系我们',
          href: '/contact'
        }
      ]
    };
  },
  created() {
    this.fetchCaseData(); // 根据ID获取案例详情
  },
  methods: {
    fixImgUrl(path) {
      if (!path) return '';
      return path.startsWith(process.env.VUE_APP_BASE_API) ? encodeURI(path) : encodeURI(process.env.VUE_APP_BASE_API + path);
    },
    async fetchCaseData() {
      this.loading = true // 开始加载
      this.error = null   // 清除旧错误
      try {
        // 从路由参数中获取ID
        const id = this.$route.params.id;
        if (!id) {
          this.error = '未找到有效的案例ID';
          console.error('No ID found in route parameters');
          return;
        }
        console.log('正在获取案例ID:', id); // 调试
        // const response = await listCases(id);
        getCases(id).then(response => {
          this.form = response.data;
        console.log('获取案例详情成功:', response);
        // 假设返回的数据直接就是案例对象
        this.casesList = response;
        });
      } catch (error) {
        this.error = '获取案例详情失败，请稍后重试';
        console.error('获取案例详情失败:', error);
      }finally {
        this.loading = false // 结束加载
      }
    },
    handleMenuClick(item, event) {
      // 如果有子菜单，阻止跳转，用于展开/收起
      if (item.children) {
        event.preventDefault();
        // 可以在这里添加展开/收起逻辑（比如维护一个展开状态数组）
        console.log('Toggle submenu for:', item.title);
      }
      // 否则允许正常跳转
    }
  }
}
</script>

<style scoped>
/* 全局重置与字体 */
#app {
  font-family: 'Poppins', sans-serif;
  margin: 0;
  padding: 0;
}

/* 容器居中，类似 Bootstrap */
.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  box-sizing: border-box;
}

/* =============
   Header 样式
============= */
.header {
  background-color: #ffffff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding: 15px 0;
  position: relative;
  z-index: 1000;
}

.header.can-sticky {
  position: sticky;
  top: 0;
  transition: all 0.3s ease;
}

.logo img {
  height: 50px;
  transition: all 0.3s ease;
}

.logo img:hover {
  opacity: 0.9;
}

/* 导航栏 - 主菜单 */
.navigation {
  display: flex;
  justify-content: center;
}

.main-menu {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  gap: 30px;
  align-items: center;
}

.main-menu > .menu-item {
  position: relative;
}

.main-menu > .menu-item > a {
  text-decoration: none;
  color: #333;
  font-weight: 500;
  font-size: 16px;
  transition: color 0.3s ease;
  padding: 5px 0;
  display: block;
}

.main-menu > .menu-item > a:hover,
.main-menu > .menu-item > a.active {
  color: #0066ff;
}

/* 二级菜单 */
.sub-menu {
  position: absolute;
  top: 100%;
  left: 0;
  background: #fff;
  min-width: 200px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
  opacity: 0;
  visibility: hidden;
  transform: translateY(-10px);
  transition: all 0.3s ease;
  list-style: none;
  margin: 0;
  padding: 10px 0;
  z-index: 1001;
}

.menu-item-has-children:hover > .sub-menu {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.sub-menu .menu-item {
  padding: 0;
}

.sub-menu .menu-item > a {
  color: #555;
  padding: 10px 20px;
  font-size: 15px;
  display: block;
  transition: background 0.3s ease, color 0.3s ease;
}

.sub-menu .menu-item > a:hover {
  background-color: #f5f9ff;
  color: #0066ff;
  padding-left: 24px;
}

/* 三级菜单 */
.sub-menu .sub-menu {
  left: 100%;
  top: 5px;
}

/* 登录按钮 */
.header_btn {
  background-color: #0066ff;
  color: #fff !important;
  padding: 10px 20px;
  text-decoration: none;
  border-radius: 30px;
  font-size: 14px;
  font-weight: 600;
  transition: all 0.3s ease;
  display: inline-block;
  border: none;
  outline: none;
}

.header_btn:hover {
  background-color: #0052cc;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 102, 255, 0.3);
}

/* =============
   Subheader 样式
============= */
.subheader {
  background-image: url('https://images.unsplash.com/photo-1499750310107-5fef28a66643?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  padding: 100px 0;
  color: #fff;
  position: relative;
}

.subheader::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6); /* 深色遮罩提升文字可读性 */
  z-index: 1;
}

.subheader .el-main {
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
}

.page-title {
  font-size: 3rem;
  font-weight: 700;
  margin: 0;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  letter-spacing: -0.5px;
}

.text-white {
  color: #ffffff;
}

.text-center {
  text-align: center;
}

/* =============
   响应式设计
============= */
@media (max-width: 992px) {
  .main-menu {
    gap: 20px;
  }

  .page-title {
    font-size: 2.5rem;
  }
}

@media (max-width: 768px) {
  .header {
    padding: 12px 0;
  }

  .logo img {
    height: 40px;
  }

  .main-menu {
    gap: 15px;
    flex-wrap: wrap;
    justify-content: center;
  }

  .main-menu > .menu-item {
    font-size: 14px;
  }

  .sub-menu {
    min-width: 180px;
  }

  .sub-menu .menu-item > a {
    padding: 8px 16px;
    font-size: 14px;
  }

  .page-title {
    font-size: 2rem;
  }

  .container {
    padding: 0 15px;
  }
}

@media (max-width: 576px) {
  .main-menu {
    gap: 10px;
    font-size: 14px;
  }

  .header_btn {
    padding: 8px 16px;
    font-size: 13px;
  }

  .page-title {
    font-size: 1.8rem;
  }
}

/* =============
   博客详情页样式
============= */

/* 外层间距 */
.section-padding {
  padding: 80px 0;
}

/* 容器居中，限制最大宽度 */
.blog-article {
  max-width: 1200px;
  margin: 0 auto;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.blog-article:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.15);
}

/* 封面图区域 */
.article-cover {
  position: relative;
  overflow: hidden;
  border-radius: 16px 16px 0 0;
  background-color: #f0f0f0;
}

.article-cover img {
  width: 100%;
  height: auto;
  max-height: 600px;
  object-fit: cover;
  display: block;
  transition: transform 0.5s ease;
}

.article-cover:hover img {
  transform: scale(1.05);
}

/* 文章标题区 */
.article-header {
  padding: 30px 40px 20px;
  background: #fff;
  border-bottom: 1px solid #eee;
}

.article-title {
  font-size: 2.5rem;
  font-weight: 700;
  color: #1a1a1a;
  margin: 0 0 12px 0;
  line-height: 1.2;
  letter-spacing: -0.5px;
}

.article-subtitle {
  font-size: 1.2rem;
  color: #666;
  font-weight: 400;
  margin: 0 0 20px 0;
  font-style: italic;
}

/* 元信息条 */
.article-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  font-size: 14px;
  color: #555;
}

.article-meta span {
  display: flex;
  align-items: center;
  gap: 6px;
}

.article-meta i {
  color: #0066ff;
  font-size: 12px;
}

/* 正文内容 */
.article-content {
  padding: 40px;
  color: #333;
  line-height: 1.8;
  font-size: 16px;
  word-wrap: break-word;
}

/* 富文本内容内的样式增强 */
.article-content h2 {
  font-size: 1.8rem;
  margin: 1.5em 0 1em;
  color: #222;
  font-weight: 600;
}

.article-content h3 {
  font-size: 1.5rem;
  margin: 1.3em 0 0.8em;
  color: #333;
}

.article-content p {
  margin: 1em 0;
}

::v-deep .article-content img {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  margin: 20px 0;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

.article-content blockquote {
  border-left: 4px solid #0066ff;
  background-color: #f5f9ff;
  padding: 20px 25px;
  margin: 30px 0;
  font-style: italic;
  color: #555;
  border-radius: 0 8px 8px 0;
}

.article-content ul, .article-content ol {
  margin: 1.5em 0;
  padding-left: 30px;
}

.article-content a {
  color: #0066ff;
  text-decoration: underline;
  transition: color 0.3s;
}

.article-content a:hover {
  color: #0040b0;
}

/* 文章底部信息 */
.article-footer {
  padding: 20px 40px 30px;
  background: #f9f9f9;
  border-top: 1px solid #eee;
  font-size: 14px;
  color: #666;
}

.stats small {
  color: #777;
}

.meta-info {
  font-size: 12px;
  color: #999;
  margin-top: 10px;
}

/* 分享区域 */
.post_share {
  margin-top: 30px;
  padding: 20px 40px;
  background: #f9f9f9;
  border-radius: 12px;
  border: 1px solid #eee;
  display: flex;
  align-items: center;
}

.post_share h6 {
  margin: 0 15px 0 0;
  font-size: 16px;
  color: #333;
  font-weight: 600;
}

.social {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  gap: 12px;
}

.social a {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #f0f0f0;
  color: #555;
  text-decoration: none;
  transition: all 0.3s ease;
}

.social a:hover {
  background: #0066ff;
  color: #fff;
  transform: translateY(-3px);
  box-shadow: 0 4px 10px rgba(0, 102, 255, 0.3);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .section-padding {
    padding: 60px 0;
  }

  .blog-article {
    border-radius: 12px;
    margin: 15px;
  }

  .article-header,
  .article-content,
  .article-footer,
  .post_share {
    padding: 20px;
  }

  .article-title {
    font-size: 2rem;
  }

  .article-meta {
    flex-direction: column;
    gap: 8px;
  }

  .post_share {
    flex-direction: column;
    align-items: flex-start;
  }

  .post_share h6 {
    margin-bottom: 10px;
  }
}

@media (max-width: 480px) {
  .article-title {
    font-size: 1.8rem;
  }

  .article-meta span {
    font-size: 13px;
  }
}
</style>
